const app = getApp() //获取app实列
const ajax = require("../../utils/ajax.js")
Page({
  data: {
    time: -1,
    phone: "",
    code: ""
  },
  inputCode:function(e){
    //验证码绑定
    this.setData({
      code:e.detail.value
    })
  },
  getPhoneNumber:function(e){
    if(!e.detail.iv){
      return 
    }
    let that=this
    wx.showLoading({
      title: '获取信息中..',
    })
    wx.login({
      success:function(res){
        if(res.code){
          ajax.request({
            url: app.url.miniGetCode,
            data:{
              code:res.code
            }
          }).then(data=>{
            if(data.code!==200){
              return wx.showToast({
                title: data.msg,
                icon:"none"
              })
            }
            wx.setStorageSync("openid", data.data.openid)
            ajax.request({
              url: app.url.miniAuth,
              data:{
                sessionKey :data.data.session_key,
                iv:e.detail.iv,
                encryptedData:e.detail.encryptedData
              }
            }).then(phone=>{
              if(phone.code!==200){
                return wx.showToast({
                  title: phone.msg,
                  icon: "none"
                })
              }
              that.loginByPhone(phone.data.phoneNumber)
            })
          })
        }
      }      
    })
  },
  loginByPhone:function(phone){
    let that = this;
    ajax.request({
      url: app.url.loginByPhone,
      data: {
        phone: phone,
      }
    }).then(res => {
      wx.setStorageSync(
        "token",
        res.token
      )
      ajax.request({
        url: app.url.userInfo,
        data: {}
      }).then(res => {
        if (res.code != 200) {
          return wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
        let str = JSON.stringify(res.data)
        wx.setStorageSync("userInfo", str)
        if (res.data.idcard == "") {
          wx.reLaunch({
            url: '../selectType/selectType',
          })
        } else {
          if (res.data.doctor_id > 0) {
            if (res.data.doctor_status == 0) {
              return wx.showToast({
                title: '您的信息已提交，妙手春花会在2个工作日内给您回复，请耐心等待，谢谢！',
                icon: 'none',
                success: function () {
                  wx.removeStorageSync("token")
                  wx.removeStorageSync("userInfo")
                }
              })
            } else if (res.data.doctor_status == 2) {
              return wx.showToast({
                title: '信息驳回',
                icon: 'none',
                success: function () {
                  wx.reLaunch({
                    url: '/pages/serverRelName/serverRelName',
                  })
                }
              })
            } else if (res.data.doctor_status == 3) {
              return wx.showToast({
                title: '该用户已禁用',
                icon: 'none',
                success: function () {
                  wx.removeStorageSync("token")
                  wx.removeStorageSync("userInfo")
                }
              })
            }
            wx.reLaunch({
              url: '/pages/serverPages/index/index',
            })
          } else {
            wx.reLaunch({
              url: '/pages/userPages/index/index',
            })
          }
        }
        wx.hideLoading()
      })
    })
  },
  submitClick: function () {
    let that = this;
    if(that.data.code.length!=6){
      return wx.showToast({
        title: '验证码格式不正确',
        icon:"none" 
      })
    }
    ajax.request({
      url: app.url.login,
      data: {
        phone: that.data.phone,
        code: that.data.code
      }
    }).then(res => {
      if(res.code!==200){
        return wx.showToast({
          title: res.msg,
          icon:"none"
        })
      }
      wx.setStorageSync(
        "token",
        res.token
      )
      ajax.request({
        url: app.url.userInfo,
        data: {}
      }).then(res => {
        if (res.code != 200) {
          return wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
        let str = JSON.stringify(res.data)
        wx.setStorageSync("userInfo", str)
        if (res.data.idcard == "") {
          wx.redirectTo({
            url: '../selectType/selectType',
          })
        } else {
          if (res.data.doctor_id > 0) {
            if(res.data.doctor_status == 0){
              return wx.showToast({
                title: '信息待审核',
                icon:'none',
                success: function () {
                  wx.removeStorageSync("token")
                  wx.removeStorageSync("userInfo")
                }
              })
            } else if (res.data.doctor_status == 2){
              return wx.showToast({
                title: '信息驳回',
                icon: 'none',
                success:function(){
                  wx.reLaunch({
                    url: '/pages/serverRelName/serverRelName',
                  })
                }
              })
            } else if (res.data.doctor_status == 3) {
              return wx.showToast({
                title: '该用户已禁用',
                icon: 'none',
                success: function () {
                  wx.removeStorageSync("token")
                  wx.removeStorageSync("userInfo")
                }
              })
            }
            wx.reLaunch({
              url: '/pages/serverPages/index/index',
            })
          } else {
            wx.reLaunch({
              url: '/pages/userPages/index/index',
            })
          }
        }
      })
    })
  },
  inputChange: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  getCode: function () {
    let that = this;
    //获取手机的验证码
    this.setData({
      time: 60
    })
    let timer = setInterval(() => {
      this.setData({
        time: this.data.time - 1
      })
      if (this.data.time < 0) {
        clearInterval(timer)
      }
    }, 1000)
    // console.log(that.data.phone)
    ajax.request({
      url: app.url.code,
      data: {
        phone: that.data.phone
      }
    }).then(res => {
      if(res.code != 200){
        clearInterval(timer)
        that.setData({
          time: 0
        })
        return wx.showToast({
          title: res.msg,
          icon:"none"
        });
      }
      if (res.smscode){
        that.setData({
          code:res.smscode+""
        })
      }
     wx.showToast({
       title: '验证码已发送',
       icon:"none"
     })
    })
  },
  onLoad: function (options) { }
})